package one.stand.controller;

import com.jmp.autocofiguration.util.OssUtil2;
import com.jmp.service.api.admin.BannerApi;
import com.jmp.service.api.admin.dto.*;
import one.stand.advice.ResultEnum;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CompanyBannerMapperExt;
import one.stand.model.CompanyBanner;
import one.stand.model.ResultModel;
import one.stand.service.GoodsSelectServcie;
import one.stand.util.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@RestController
public class BannerController implements BannerApi {
    @Autowired
    private CompanyBannerMapperExt companyBannerMapperExt;
    @Autowired
    private GoodsSelectServcie goodsSelectServcie;

    @Override
    public ResultModel save(@RequestBody BannerSaveDto request) {
        CompanyBanner companyBanner = new CompanyBanner();
        companyBanner.setId(request.getId());
        companyBanner.setBannerType(request.getBannerType());
        companyBanner.setCompanyId(request.getCompanyId());
        companyBanner.setSource(request.getSource());
        companyBanner.setType(request.getType());
        companyBanner.setEvent(request.getEvent());
        companyBanner.setEventId(request.getEventId());
        companyBanner.setSort(request.getSort());
        companyBanner.setRefId(request.getRefId());
        companyBanner.setMemo(request.getMemo());

        if (EVENT_NULL != companyBanner.getEvent()
                && EVENT_GOODS_LIST != companyBanner.getEvent()
                && EVENT_SUBJECT != companyBanner.getEvent()
                && EVENT_TICKET != companyBanner.getEvent()
        ) {
            AssertUtil.nonNull(companyBanner.getEventId(), ResultEnum.PARAM_CHECK, "事件ID不能为空");
        }

        companyBanner.setSource(OssUtil2.getWebsiteUrlReplace(companyBanner.getSource()));
        if (StringUtils.isNoneBlank(companyBanner.getSource())) {
            OssUtil2.ImgInfo imgInfo = OssUtil2.info(companyBanner.getSource());
            if (Objects.nonNull(imgInfo)) {
                companyBanner.setWidth(imgInfo.getImageWidth());
                companyBanner.setHeight(imgInfo.getImageHeight());
            }
        }

        if (Objects.isNull(companyBanner.getId())) {
            companyBanner.setCreateTime(new Date());
            companyBannerMapperExt.insertSelective(companyBanner);

            goodsSelectServcie.updateBanner(companyBanner.getCompanyId(), null);
        } else {
            CompanyBanner companyBannerSelect = companyBannerMapperExt.selectByPrimaryKey(companyBanner.getId());
            AssertUtil.nonNull(companyBannerSelect, ResultEnum.PARAM_CHECK, "BANNER不存在");
            AssertUtil.isNull(companyBannerSelect.getSelectBannerId(), ResultEnum.PARAM_CHECK, "优选Banner不能编辑");

            if (EVENT_NULL == companyBanner.getEvent()
                    || EVENT_TICKET == companyBanner.getEvent()) {
                companyBanner.setEventId(null);
            }
            companyBanner.setCompanyId(null);
            companyBanner.setBannerType(null);
            companyBanner.setUpdateTime(new Date());
            companyBanner.setStatus(null);
            companyBanner.setSelectBannerId(null);
            companyBanner.setSelectStatus(null);
            companyBannerMapperExt.updateByPrimaryKeySelective(companyBanner);

            goodsSelectServcie.updateBanner(companyBannerSelect.getCompanyId(), null);
        }

        if (EVENT_SUBJECT == companyBanner.getEvent()
                || EVENT_GOODS_LIST == companyBanner.getEvent()) {
            CompanyBanner companyBannerUpdate = new CompanyBanner();
            companyBannerUpdate.setId(companyBanner.getId());
            companyBannerUpdate.setEventId(String.valueOf(companyBanner.getId()));
            companyBannerMapperExt.updateByPrimaryKeySelective(companyBannerUpdate);
        }

        return ResultModel.success();
    }

    @Override
    public ResultModel remove(@RequestBody BannerRemoveDto request) {
        CompanyBanner companyBanner = companyBannerMapperExt.selectByPrimaryKey(request.getId());
        AssertUtil.nonNull(companyBanner, ResultEnum.PARAM_CHECK);
        AssertUtil.isNull(companyBanner.getSelectBannerId(), ResultEnum.PARAM_CHECK, "优选Banner不能删除，请下架");

        companyBannerMapperExt.deleteByPrimaryKey(request.getId());

        goodsSelectServcie.updateBanner(null, request.getId());

        return ResultModel.success();
    }

    @Override
    public ResultModel up(@RequestBody BannerUpDto request) {
        CompanyBanner banner = new CompanyBanner();
        banner.setId(request.getId());
        banner.setStatus(STATUS_UP);
        banner.setSelectStatus(SELECT_STATUS_NONE);
        companyBannerMapperExt.updateByPrimaryKeySelective(banner);

        CompanyBanner companyBanner = companyBannerMapperExt.selectByPrimaryKey(request.getId());
        goodsSelectServcie.updateBanner(companyBanner.getCompanyId(), null);

        return ResultModel.success();
    }

    @Override
    public ResultModel down(@RequestBody BannerDownDto request) {

        CompanyBanner banner = new CompanyBanner();
        banner.setId(request.getId());
        banner.setStatus(STATUS_DOWN);
        CompanyBanner companyBanner = companyBannerMapperExt.selectByPrimaryKey(request.getId());
        if (Objects.nonNull(companyBanner.getSelectBannerId())) {// 优选商品
            banner.setSelectStatus(SELECT_STATUS_DOWN);
        }
        companyBannerMapperExt.updateByPrimaryKeySelective(banner);

        goodsSelectServcie.updateBanner(null, request.getId());

        return ResultModel.success();
    }

    @Override
    public ResultModel<BannerListVo> list(@RequestBody BannerListDto request) {
        request.startPage();
        List<CompanyBanner> companyBannerList = companyBannerMapperExt.selectByCompanyId(request.getCompanyId(), request.getBannerType(), request.getRefId());
        PageResponse2<BannerListVo.Banner> bannerPage = ConvertUtils.page2(companyBannerList, companyBanner -> {
            return BannerListVo.Banner.builder()
                    .id(companyBanner.getId())
                    .companyId(companyBanner.getCompanyId())
                    .bannerType(companyBanner.getBannerType())
                    .source(companyBanner.getSource())
                    .type(companyBanner.getType())
                    .event(companyBanner.getEvent())
                    .eventId(companyBanner.getEventId())
                    .memo(companyBanner.getMemo())
                    .sort(companyBanner.getSort())
                    .status(companyBanner.getStatus())
                    .createTime(companyBanner.getCreateTime())
                    .refId(companyBanner.getRefId())
                    .width(companyBanner.getWidth())
                    .height(companyBanner.getHeight())
                    .selectBannerId(companyBanner.getSelectBannerId())
                    .build();
        });

        BannerListVo vo = BannerListVo.builder()
                .bannerPage(bannerPage)
                .build();
        return ResultModel.success(vo);
    }
}
